import {
  VariablesRequest,
  VariablesInfo,
  CreateVariablesRequest,
  ModifyVariablesRequest,
  DeleteVariablesRequest,
  InterfacesRequest,
  InterfacesInfo,
  DeleteInterfacesRequest,
} from 'admin/schema/BasicResource';
import {
  BasicClassRequest,
  BasicClassInfo,
  CreateWordClassRequest,
  ModifyWordClassRequest,
  DeleteWordClassRequest,
  AtomicWordRequest,
  AtomicWordInfo,
  CreateAtomicWordRequest,
  ModifyAtomicWordRequest,
  DeleteAtomicWordRequest,
} from 'admin/schema/BasicWord';
import { FlowAttributeInfo, FlowDebugRequest } from 'admin/schema/FlowDebug';
import { FlowGlobalInfo } from 'admin/schema/FlowGlobalSet';
import {
  IntensionRequest,
  IntentionLibraryInfo,
  DeleteIntentionRequest,
  CreateIntentionRequest,
  ModifyIntentionRequest,
  UploadProgressInfo,
} from 'admin/schema/IntentionLibrary';
import {
  QuestionAnswerInfoRequest,
  QuestionAnswerInfo,
  AddQaRequest,
  ModifyQaRequest,
  GetQaInfo,
  FlowNodesResponse,
} from 'admin/schema/QuestionAnswer';
import {
  RobotsRequest,
  RobotInfo,
  CreateRobotRequest,
  ModifyRobotRequest,
} from 'admin/schema/Robots';
import {
  RobotVersionRequest,
  RobotVersion,
  CreateVersionRequest,
  PutVersionRequest,
  GetRobotInfo,
} from 'admin/schema/RobotVersion';
import {
  SoundTempRequest,
  SoundTempInfo,
  CreateSoundTempRequest,
  EditSoundTempRequest,
  SoundrecordRequest,
  SoundRecordInfo,
} from 'admin/schema/SoundLibrary';
import {
  AddWebInterfaceRequest,
  GetWebInterfaceInfo,
  ModifyWebInterfaceRequest,
} from 'admin/schema/WebInterface';
import {
  AddWordModelRequest,
  AutoWordModelRequest,
  AutoWordModel,
  ModifyWordModelRequest,
  WordModelsRequest,
  WordModelsInfo,
  DeleteWordModelRequest,
} from 'admin/schema/WordModel';
import $http from '../../../servers/http';
import { Result } from '../../schema/Result';
/* 机器人管理
---------------------------------------------------------------- */
/* 机器人版本相关
---------------------------------------------------------------- */

/**
 * 查询机器人版本列表
 * @param data RobotVersionRequest
 */
export const getVersions = (data: RobotVersionRequest): Promise<Result<RobotVersion[]>> => {
  return $http.get({
    url: '/robotmanage/versions',
    data,
  });
};

/**
 * 新建机器人版本
 * @param data CreateVersionRequest
 */
export const postVersion = (data: CreateVersionRequest): Promise<Result<{ verisonId: number }>> => {
  return $http.post({
    url: '/robotmanage/versions',
    data,
  });
};

/**
 * 编辑机器人版本
 * @param data PutVersionRequest
 */
export const putVersion = (data: PutVersionRequest): Promise<Result<null>> => {
  return $http.put({
    url: `/robotmanage/versions/${data.versionId}`,
    data,
  });
};

/**
 * 下线
 */
export const offVersion = (data: { versionId: number }): Promise<Result<null>> => {
  return $http.patch({
    url: `/robotmanage/versions/${data.versionId}/status/off`,
  });
};

/**
 * 上线
 */
export const onVersion = (data: { versionId: number }): Promise<Result<null>> => {
  return $http.patch({
    url: `/robotmanage/versions/${data.versionId}/status/on`,
  });
};

/**
 * 机器人版本复制
 */
export const copyVersion = (data: { versionId: number }): Promise<Result<null>> => {
  return $http.patch(
    {
      url: `/robotmanage/versions/${data.versionId}/copy`,
    },
    { timeout: 600000 }
  );
};

/**
 * 删除版本
 */
export const deleteVersion = (data: { versionId: number }): Promise<Result<null>> => {
  return $http.delete({
    url: `/robotmanage/versions/${data.versionId}`,
  });
};

/**
 * 查询机器人线上版本
 */
export const getRobotInfo = (data: { robotId: number }): Promise<Result<GetRobotInfo>> => {
  return $http.get({
    url: `/robotmanage/robots/${data.robotId}`,
  });
};

/* 机器人相关
---------------------------------------------------------------- */

/**
 * 查询机器人列表
 * @param data RobotsRequest
 */
export const getRobots = (data: RobotsRequest): Promise<Result<RobotInfo[]>> => {
  return $http.get({
    url: '/robotmanage/robots',
    data,
  });
};

/**
 * 查询在线的机器人
 * @param data
 */
export const getOnlineRobot = (data: RobotsRequest): Promise<Result<RobotInfo[]>> => {
  return $http.get({
    url: '/robotmanage/robots/online',
    data,
  });
};

/**
 * 分页查询机器人列表-无数据权限
 */
export const getRobotsWithoutPermission = (data: RobotsRequest): Promise<Result<RobotInfo[]>> => {
  return $http.get({
    url: `/robotmanage/robots/canceldatapermission`,
    data,
  });
};
/**
 * 新建机器人
 * @param data CreateRobotRequest
 */
export const createRobot = (data: CreateRobotRequest): Promise<Result<{}>> => {
  return $http.post({
    url: '/robotmanage/robots',
    data,
  });
};

/**
 * 编辑机器人
 */
export const modifyRobot = (id: number, data: ModifyRobotRequest): Promise<Result<{}>> => {
  return $http.put({
    url: `/robotmanage/robots/${id}`,
    data,
  });
};

/**
 * 删除机器人
 */
export const deleteRobot = (id: number): Promise<Result<{}>> => {
  return $http.delete({
    url: `/robotmanage/robots/${id}`,
  });
};
/**
 * 导入机器人
 * @param data any
 */
export const uploadRobot = (data: any): Promise<Result<{}>> => {
  return $http.post(
    {
      url: `/robotmanage/robots/import`,
      data,
    },
    { timeout: 600000 }
  );
};
/**
 * 导入机器人-成功-确定
 * @param robotId
 */
export const confirmUploadRobot = (robotId: number): Promise<Result<{}>> => {
  return $http.post({
    url: `/robotmanage/robots/importconfirm/${robotId}`,
  });
};
/**
 * 导出机器人
 * @param data any
 */
export const downloadRobot = (id: any): Promise<any> => {
  return $http.get(
    {
      url: `/robotmanage/robots/${id}/export`,
    },
    {
      responseType: 'blob', // 声明返回blob格式
      onDownloadProgress: (progress: any) => {
        // 下载接口
        console.log(progress, '下载接口');
        // console.log(Math.round((progress.loaded / progress.total) * 100) + "%");
      },
    }
  );
};
/* 录音库相关
---------------------------------------------------------------- */
/**
 * 根据类型-查询录音模板列表
 * 用于创建任务时，选择录音模板，入参是机器人id，不是版本id
 * @param templateType 0：机器人类型；1：流程通知类型
 * @param data SoundTempRequest
 */
export const getSoundTempListByType = (
  templateType: number,
  data: SoundTempRequest
): Promise<Result<SoundTempInfo[]>> => {
  return $http.get({
    url: `/robotmanage/soundrecordtemplates/info/${templateType}`,
    data,
  });
};

/**
 * 查询录音模板列表（1.机器人版本Id，2.通知流程Id）
 * @param data SoundTempRequest
 */
export const getSoundTempList = (data: SoundTempRequest): Promise<Result<SoundTempInfo[]>> => {
  return $http.get({
    url: '/robotmanage/soundrecordtemplates',
    data,
  });
};

/**
 * 创建录音模板
 * @param data CreateSoundTempRequest
 */
export const createSoundTemp = (
  data: CreateSoundTempRequest
): Promise<Result<{ templateId: number }>> => {
  return $http.post({
    url: '/robotmanage/soundrecordtemplates',
    data,
  });
};

/**
 * 删除录音模板
 * @param templateId
 */
export const deleteSoundTemp = (templateId: number): Promise<Result<null>> => {
  return $http.delete({
    url: `/robotmanage/soundrecordtemplates/${templateId}`,
  });
};

/**
 * 修改录音模板
 * @param templateId
 * @param data EditSoundTempRequest
 */
export const editSoundTemp = (
  templateId: number,
  data: EditSoundTempRequest
): Promise<Result<null>> => {
  return $http.put({
    url: `/robotmanage/soundrecordtemplates/${templateId}`,
    data,
  });
};

/**
 * 查询录音模板-话术列表
 * @param data SoundrecordRequest
 */
export const getSoundRecordList = (
  data: SoundrecordRequest
): Promise<Result<{ list: SoundRecordInfo[]; filePath: string }>> => {
  return $http.get({
    url: '/robotmanage/soundrecords',
    data,
  });
};

/**
 * 录音库-上传录音
 * @param soundRecordId
 * @param data UploadSoundRecordRequest
 */
export const uploadSoundRecord = (soundRecordId: number, data: any): Promise<Result<null>> => {
  return $http.post({
    url: `/robotmanage/soundrecords/upload/${soundRecordId}`,
    data,
  });
};

/**
 * 录音库-删除录音
 * @param soundRecordId
 */
export const deleteSoundRecord = (soundRecordId: number): Promise<Result<null>> => {
  return $http.post({
    url: `/robotmanage/soundrecords/${soundRecordId}`,
  });
};

/* 意图相关
---------------------------------------------------------------- */

/**
 * 查询意图列表
 * @param data IntentionsRequest
 */
export const getIntentions = (data: IntensionRequest): Promise<Result<IntentionLibraryInfo[]>> => {
  return $http.get({
    url: '/robotmanage/intentions',
    data,
  });
};

/**
 * 意图组件-查询意图列表(包含“肯定”，“否定”两种固定的特殊意图)
 * @param data IntentionsRequest
 */
export const getIntentionsAll = (
  data: IntensionRequest
): Promise<Result<IntentionLibraryInfo[]>> => {
  return $http.get({
    url: '/robotmanage/intentions/all',
    data,
  });
};

/**
 * 查询意图列表导入进度
 * @param versionId
 */
export const getIntentionsUploadInfo = (versionId: number): Promise<Result<UploadProgressInfo>> => {
  return $http.get({
    url: `/robotmanage/intentions/progress/${versionId}`,
  });
};

/**
 * 导入意图-成功-确定
 * @param robotId
 */
export const confirmUploadIntention = (versionId: number): Promise<Result<{}>> => {
  return $http.post({
    url: `/robotmanage/intentions/importconfirm/${versionId}`,
  });
};

/**
 * 删除意图
 * @param data DeleteIntentionRequest
 */
export const deleteIntention = (data: DeleteIntentionRequest): Promise<Result<{}>> => {
  return $http.delete({
    url: `/robotmanage/intentions/${data.intentionId}`,
    data,
  });
};

/**
 * 创建意图
 * @param data CreateIntentionRequest
 */
export const createIntention = (
  data: CreateIntentionRequest
): Promise<Result<{ intentionId: number }>> => {
  return $http.post({
    url: '/robotmanage/intentions',
    data,
  });
};
/**
 * 编辑意图
 * @param data ModifyIntentionRequest
 */
export const modifyIntention = (data: ModifyIntentionRequest): Promise<Result<{}>> => {
  return $http.put({
    url: `/robotmanage/intentions/${data.intentionId}`,
    data,
  });
};
/**
 * 查询问答列表导入进度
 * @param versionId
 */
export const getQAUploadInfo = (versionId: number): Promise<Result<UploadProgressInfo>> => {
  return $http.get({
    url: `/robotmanage/questionanswers/progress/${versionId}`,
  });
};
/**
 * 导入问答-成功-确定
 * @param robotId
 */
export const confirmUploadQuestion = (versionId: number): Promise<Result<{}>> => {
  return $http.post({
    url: `/robotmanage/questionanswers/importconfirm/${versionId}`,
  });
};

/**批量导入:问题，意图 */
export const uploadBatchList = (
  mode: 'intention' | 'question',
  versionId: number,
  data: any // 选择的导入文件
): Promise<any> => {
  let api_path = '';
  switch (mode) {
    case 'intention':
      api_path = '/robotmanage/intentions/import/' + versionId;
      break;

    case 'question':
      api_path = '/robotmanage/questionanswers/import/' + versionId;
      break;

    default:
      break;
  }
  return $http.post({ url: api_path, data }, { timeout: 600000 });
};
/**批量导出:问题，意图 */
export const downloadBatchList = (
  mode: 'intention' | 'question',
  versionId: number
): Promise<any> => {
  let api_path = '';
  switch (mode) {
    case 'intention':
      api_path = '/robotmanage/intentions/export/' + versionId;
      break;

    case 'question':
      api_path = '/robotmanage/questionanswers/export/' + versionId;
      break;

    default:
      break;
  }
  return $http.get(
    { url: api_path },
    {
      responseType: 'blob', // 声明返回blob格式
      onDownloadProgress: (progress: any) => {
        // 下载接口
        console.log(progress, '下载接口');
        // console.log(Math.round((progress.loaded / progress.total) * 100) + "%");
      },
    }
  );
};

/**
 * 机器人版本(意图库、问答库)-语义批量学习
 */
export const batchUnderstandByVersion = (
  versionId: number,
  intentionFlag: 'intention' | 'question'
): Promise<Result<any>> => {
  return $http.put({
    url: `/robotmanage/extendquestions/batchunderstand/${versionId}/${intentionFlag}`,
  });
};

/**
 * 机器人版本(意图库、问答库)-语义批量学习-进度查询
 */
export const getBatchStudyProgressByVersion = (
  versionId: number,
  intentionFlag: 'intention' | 'question'
): Promise<
  Result<{
    totalNumber: number;
    finishedNumber: number;
    isFinish: boolean;
  } | null>
> => {
  return $http.get({
    url: `/robotmanage/extendquestions/versionbatchunderstandprogress/${versionId}/${intentionFlag}`,
  });
};

/**
 * 批量学习-成功-确定
 * @param robotId
 */
export const confirmStudyIntention = (
  versionId: number,
  intentionFlag: 'intention' | 'question'
): Promise<Result<{}>> => {
  return $http.put({
    url: `/robotmanage/extendquestions/versionbatchunderstandprogress/${versionId}/${intentionFlag}`,
  });
};
/* 词模相关
---------------------------------------------------------------- */

/**
 * 创建词模
 */
export const addWordModel = (
  data: AddWordModelRequest
): Promise<Result<{ wordModelId: number }>> => {
  return $http.post({
    url: '/robotmanage/wordmodels',
    data,
  });
};

/**
 * 自动生成词模
 */
export const autoWordModel = (data: AutoWordModelRequest): Promise<Result<AutoWordModel[]>> => {
  return $http.post({
    url: '/robotmanage/wordmodels/auto',
    data,
  });
};

/**
 * 编辑词模
 */
export const modifyWordModel = (data: ModifyWordModelRequest): Promise<Result<null>> => {
  return $http.post({
    url: `/robotmanage/wordmodels/${data.wordModelId}`,
    data,
  });
};

/**
 * 查询词模列表
 * @param data WordModelsRequest
 */
export const getWordModels = (data: WordModelsRequest): Promise<Result<WordModelsInfo[]>> => {
  return $http.get({
    url: '/robotmanage/wordmodels',
    data,
  });
};

/**
 * 删除词模
 * @param data DeleteWordModelRequest
 */
export const deleteWordModel = (data: DeleteWordModelRequest): Promise<Result<{}>> => {
  return $http.delete({
    url: `/robotmanage/wordmodels/${data.wordModelId}`,
    data,
  });
};

/* 问答库相关
---------------------------------------------------------------- */
/**
 * 查询问答库列表
 */
export const getQuestionAnswers = (
  data: QuestionAnswerInfoRequest
): Promise<Result<QuestionAnswerInfo[]>> => {
  return $http.get({
    url: '/robotmanage/questionanswers',
    data,
  });
};

/**
 * 问答开关
 */
export const switchQuestionAnswer = (data: {
  /**
   * 状态开关  (on-开;  off-关)
   */
  questionSwitch: 'on' | 'off';
  /**
   * 问答主键
   */
  questionId: number;
}): Promise<Result<null>> => {
  return $http.post({
    url: `/robotmanage/questionanswers/${data.questionId}/switch`,
    data,
  });
};

/**
 * 删除问答
 */
export const deleteQa = (data: {
  /**
   * 问答主键
   */
  questionId: number;
}): Promise<Result<null>> => {
  return $http.delete({
    url: `/robotmanage/questionanswers/${data.questionId}`,
  });
};

/**
 * 创建问答
 */
export const addQa = (data: AddQaRequest): Promise<Result<{ questionId: number }>> => {
  return $http.post({
    url: '/robotmanage/questionanswers',
    data,
  });
};

/**
 * 编辑问答库
 */
export const modifyQa = (data: ModifyQaRequest): Promise<Result<null>> => {
  return $http.put({
    url: `/robotmanage/questionanswers/${data.questionId}`,
    data,
  });
};

/**
 * 查询问答信息
 */
export const getQa = (data: { questionId: number }): Promise<Result<GetQaInfo>> => {
  return $http.get({
    url: `/robotmanage/questionanswers/${data.questionId}`,
  });
};

/**查询流程节点列表 */
export const getFlowNodes = (data: { versionId: number }): Promise<Result<FlowNodesResponse[]>> => {
  return $http.get({
    url: `/robotmanage/flows/nodes/${data.versionId}`,
  });
};

/* 基础词库相关
---------------------------------------------------------------- */

/**
 * 查询词类列表
 * @param data BasicClassRequest
 */
export const getWordList = (data: BasicClassRequest): Promise<Result<BasicClassInfo[]>> => {
  return $http.get({
    url: '/robotmanage/wordclass',
    data,
  });
};

/**
 * 新建词类
 * @param data CreateWordClassRequest
 */
export const createWordClass = (data: CreateWordClassRequest): Promise<Result<{}>> => {
  return $http.post({
    url: '/robotmanage/wordclass',
    data,
  });
};

/**
 * 编辑词类
 * @param data ModifyWordClassRequest
 */
export const modifyWordClass = (data: ModifyWordClassRequest): Promise<Result<{}>> => {
  return $http.put({
    url: `/robotmanage/wordclass/${data.wordClassId}`,
    data,
  });
};

/**
 * 删除词类
 * @param data DeleteWordClassRequest
 */
export const deleteWordClass = (data: DeleteWordClassRequest): Promise<Result<{}>> => {
  return $http.delete({
    url: `/robotmanage/wordclass/${data.wordClassId}`,
    data,
  });
};

/* 基础词库-专业词相关
---------------------------------------------------------------- */

/**
 * 查询专业词列表
 * @param data AtomicWordRequest
 */
export const getAtomicWordList = (data: AtomicWordRequest): Promise<Result<AtomicWordInfo[]>> => {
  return $http.get({
    url: '/robotmanage/atomicwords',
    data,
  });
};

/**
 * 新建专业词
 * @param data CreateAtomicWordRequest
 */
export const createAtomicWord = (data: CreateAtomicWordRequest): Promise<Result<{}>> => {
  return $http.post({
    url: '/robotmanage/atomicwords',
    data,
  });
};

/**
 * 编辑专业词
 * @param data ModifyAtomicWordRequest
 */
export const modifyAtomicWord = (data: ModifyAtomicWordRequest): Promise<Result<{}>> => {
  return $http.put({
    url: `/robotmanage/atomicwords/${data.entriesId}`,
    data,
  });
};

/**
 * 删除词类
 * @param data DeleteAtomicWordRequest
 */
export const deleteAtomicWord = (data: DeleteAtomicWordRequest): Promise<Result<{}>> => {
  return $http.delete({
    url: `/robotmanage/atomicwords/${data.entriesId}`,
    data,
  });
};

/* 基础资源-变量相关
---------------------------------------------------------------- */

/**
 * 查询变量列表
 * @param data VariablesRequest
 */
export const getVariablesList = (data: VariablesRequest): Promise<Result<VariablesInfo[]>> => {
  return $http.get({
    url: '/robotmanage/variables',
    data,
  });
};

/**
 * 新建变量
 * @param data CreateVariablesRequest
 */
export const createVariables = (data: CreateVariablesRequest): Promise<Result<{}>> => {
  return $http.post({
    url: '/robotmanage/variables',
    data,
  });
};

/**
 * 编辑变量
 * @param data ModifyVariablesRequest
 */
export const modifyVariables = (data: ModifyVariablesRequest): Promise<Result<{}>> => {
  return $http.put({
    url: `/robotmanage/variables/${data.varId}`,
    data,
  });
};

/**
 * 删除变量
 * @param data DeleteVariablesRequest
 */
export const deleteVariables = (data: DeleteVariablesRequest): Promise<Result<{}>> => {
  return $http.delete({
    url: `/robotmanage/variables/${data.varId}`,
    data,
  });
};

/* 接口相关
---------------------------------------------------------------- */

/**
 * 创建接口
 */
export const addWebInterface = (
  data: AddWebInterfaceRequest
): Promise<Result<{ interfaceId: number }>> => {
  return $http.post({
    url: '/robotmanage/interfaces',
    data,
  });
};

/**
 * 查询接口信息
 */
export const getWebInterface = (data: {
  interfaceId: number;
}): Promise<Result<GetWebInterfaceInfo>> => {
  return $http.get({
    url: `/robotmanage/interfaces/${data.interfaceId}`,
    data,
  });
};

/**
 * 编辑接口
 */
export const modifyWebInterface = (data: ModifyWebInterfaceRequest): Promise<Result<null>> => {
  return $http.put({
    url: `/robotmanage/interfaces/${data.interfaceId}`,
    data,
  });
};

/**
 * 查询接口列表
 * @param data InterfacesRequest
 */
export const getInterfacesList = (data: InterfacesRequest): Promise<Result<InterfacesInfo[]>> => {
  return $http.get({
    url: '/robotmanage/interfaces',
    data,
  });
};

/**
 * 删除接口
 * @param data DeleteInterfacesRequest
 */
export const deleteInterfaces = (data: DeleteInterfacesRequest): Promise<Result<{}>> => {
  return $http.delete({
    url: `/robotmanage/interfaces/${data.interfaceId}`,
    data,
  });
};

/* 流程相关
---------------------------------------------------------------- */

/**查询流程 */
export const getFlow = (
  versionId: number,
  data: {
    type: 'publish' | 'draft'; // 分类 publish-已发布; draft-草稿
  }
): Promise<Result<{ content: string }>> => {
  return $http.get({
    url: `/robotmanage/flows/${versionId}`,
    data,
  });
};

/**保存流程 */
export const postFlow = (data: {
  versionId: number;
  content: string;
  isPublish: boolean;
}): Promise<Result<null>> => {
  return $http.post({ url: `/robotmanage/flows`, data });
};

/**流程调试【外呼测试】 */
export const flowDebug = (
  data: FlowDebugRequest
): Promise<Result<{ callSerialNumber: string }>> => {
  return $http.post({
    url: '/robotmanage/flows/calldebug',
    data,
  });
};

/**
 * 查询流程属性列表
 */
export const getFlowAttributesList = (versionId: number): Promise<Result<FlowAttributeInfo[]>> => {
  return $http.get({
    url: `/robotmanage/flows/${versionId}/attributes`,
  });
};

const mockApi = '';
// const mockApi = 'http://172.30.66.94:3000/mock/79';
/**流程【全局设置】-查询 */
export const getFlowGlobalSet = (data: { versionId: number }): Promise<Result<FlowGlobalInfo>> => {
  return $http.get({
    url: mockApi + '/robotmanage/globalsettings',
    data,
  });
};

/**流程【全局设置】-保存 */
export const saveFlowGlobalSet = (data: FlowGlobalInfo): Promise<Result<null>> => {
  return $http.post({
    url: '/robotmanage/globalsettings',
    data,
  });
};
